package com.hnevc.exam4zhouminmin;

import android.app.DownloadManager;
import android.app.IntentService;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    //声明数据变量
    String username,password;
    //声明控件变量
    Button mBtnLogin;
    EditText mEtUserName;
    EditText mEtPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //从布局文件中获取控件
        mBtnLogin = (Button) findViewById(R.id.id_btn_login);
        mEtPassword = (EditText) findViewById(R.id.id_et_password);
        mEtUserName = (EditText) findViewById(R.id.id_et_username);

        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //获取用户输入的数据
                username = mEtUserName.getText().toString();
                password = mEtPassword.getText().toString();

                //创建线程,访问网络
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //1、访问网络，获取服务器数据
                            //（1）准备url
                        String url = "http://10.1.63.232:8080/RestaurantServer/user/doLogin";
                            //（2）准备client
                        OkHttpClient client = new OkHttpClient();
                            //（3）准备json字符串
                     
                        JSONObject object = new JSONObject();
                        try {
                            object.put("userName",username);
                            object.put("userPassword",password);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String s = object.toString();//s就是json字符串
                            //（4）构建RequestBody
                        RequestBody requestBody
                                = RequestBody.create(MediaType.parse("application/json"),s);

                            //（5）构建Request
                        Request request = new Request.Builder()
                                .url(url)
                                .post(requestBody)
                                .build();
                        try {
                            //（6）执行请求
                            Response response = client.newCall(request).execute();
                            //（7）获取网络返回的数据
                            String data = response.body().string();

                            //2、解析数据，处理数据
                            if(data.length()>0){
                                //2、1 正确，跳转到ShowActivity
                                Intent intent = new Intent();
                                intent.setClass(MainActivity.this,ShowActivity.class);
                                startActivity(intent);

                            }else {
                                //2、2 错误，提示用户输入错误
                                Toast.makeText(MainActivity.this, "输入错误", Toast.LENGTH_SHORT).show();
                            }

                        }catch (IOException e){
                            e.printStackTrace();
                        }

                    }
                }).start();

            }
        });


    }
}
